'''
学生视图
'''
from interface import student_interface
from interface import common_interface
from lib import common

user_info = {
    'user': None
}


def register():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请再次输入密码：').strip()
        if not password == re_password:
            print('用户名或者密码输入错误，请重新输入！')
            continue
        flag, msg = student_interface.register_interface(
            username, password
        )
        print(msg)
        if flag:
            break


def login():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()

        flag, msg = common_interface.login_interface(
            username, password, user_type='student'
        )
        print(msg)
        if flag:
            # 可变类型不用global
            user_info['user'] = username
            break


@common.auth('student')
def select_school():
    while True:
        flag, school_list = common_interface.get_all_school_interface()
        if not flag:
            print('没有学校可选择，请联系管理员！')
            break

        for index, school_name in enumerate(school_list):
            print(index, school_name)

        choice = input("请输入学校编号：").strip()

        if not choice.isdigit():
            print("请输入数字编号！")
            continue
        choice = int(choice)
        if choice not in range(len(school_list)):
            print("请输入正确的学校编号！")
            continue

        flag, msg = student_interface.select_school(
            school_list[choice], user_info['user']
        )
        print(msg)
        break


@common.auth('student')
def select_course():
    while True:
        # 获取课程
        flag, course_list = student_interface.get_course_list_interface(
            user_info.get('user')
        )
        if not flag:
            print(course_list)
            break

        for index, course_name in enumerate(course_list):
            print(index, course_name)

        choice = input('请输入课程编号：').strip()
        if not choice.isdigit():
            print('请输入数字编号！')
            continue
        choice = int(choice)
        if choice not in range(len(course_list)):
            print('请输入正确的编号！')
            continue

        flag, msg = student_interface.select_course_interface(
            course_list[choice], user_info.get('user')
        )
        print(msg)
        if flag:
            break


@common.auth('student')
def check_score():
    score_dict = student_interface.check_score_interface(
        user_info.get('user')
    )
    if not score_dict:
        print('没有选择课程')

    else:
        print(score_dict)


student_dic = {
    '1': register,
    '2': login,
    '3': select_school,
    '4': select_course,
    '5': check_score
}


def view():
    while True:
        print('''
        ====== 欢迎来到选课系统 ======
                 1.注册
                 2.登录
                 3.选择校区
                 4.选择课程
                 5.查看分数
        =========== end ===========
        ''')

        choice = input('请输入功能编号：').strip()
        if choice not in student_dic:
            print("请输入正确的编号！")
            continue

        student_dic.get(choice)()
